package kitrobot.com.bed.Activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONObject;

import kitrobot.com.bed.CountDownUtil;
import kitrobot.com.bed.Service.IdentifyingService;
import kitrobot.com.bed.Service.LoginService;
import kitrobot.com.bed.Service.TransferService;
import kitrobot.com.wechat_bottom_navigation.R;

public class TransferActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText et_ptt;
    private TextView tv_gyan;
    private EditText et_pty;
    private CheckBox cb_qc;
    private Context mContext;
    private String bid;
    private String token;
    private int countSeconds = 60;
    private Handler mCountHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (countSeconds > 0) {
                --countSeconds;
                tv_gyan.setText("(" + countSeconds + ")后获取验证码");
                mCountHandler.sendEmptyMessageDelayed(0, 1000);
            } else {
                countSeconds = 60;
                tv_gyan.setText("请重新获取验证码");
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.power_transfer);
        et_ptt= (EditText) findViewById(R.id.et_ptt);
        tv_gyan= (TextView) findViewById(R.id.tv_gyan);
        et_pty= (EditText) findViewById(R.id.et_pty);
        cb_qc= (CheckBox) findViewById(R.id.cb_qc);
        Intent intent=getIntent();
        bid=intent.getStringExtra("bid");
        SharedPreferences sp = getSharedPreferences("loginToken",
                Context.MODE_PRIVATE);
        token = sp.getString("token", "");
    }
    public void Yanzhengto(View view) {
        final String phone=et_ptt.getText().toString().trim();
        String a= String.valueOf(isMobileNO( phone));
        if (phone.length()==0){
            Toast.makeText(TransferActivity.this,"手机号码不能为空！",Toast.LENGTH_LONG).show();
        }
        else if(a.equals("false")||phone.length()<11){
            Toast.makeText(TransferActivity.this,"请输入正确的手机号码！",Toast.LENGTH_LONG).show();
        }
        else {
            new Thread(){
                public void run(){
                    final String result= IdentifyingService.IdentifyingByClientPost(phone);
                    if (result!=null){
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {

                                Toast.makeText(TransferActivity.this,result,Toast.LENGTH_LONG).show();

                            }
                        });
                    }
                    else{
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(TransferActivity.this,"请求失败",Toast.LENGTH_LONG).show();
                            }
                        });
                    }
                };

            }.start();
            Jishi();
        }
    }
    //计时60秒
    public void Jishi(){
        new CountDownUtil(tv_gyan)

                .setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            final String phone=et_ptt.getText().toString().trim();
                                            String a= String.valueOf(isMobileNO( phone));
                                            if (phone.length()==0){
                                                Toast.makeText(TransferActivity.this,"手机号码不能为空！",Toast.LENGTH_LONG).show();
                                            }
                                            else if(a.equals("false")||phone.length()<11){
                                                Toast.makeText(TransferActivity.this,"请输入正确的手机号码！",Toast.LENGTH_LONG).show();
                                            }
                                            else {
                                                new Thread(){
                                                    public void run(){
                                                        final String result= IdentifyingService.IdentifyingByClientPost(phone);
                                                        if (result!=null){
                                                            runOnUiThread(new Runnable() {
                                                                @Override
                                                                public void run() {
                                                                    Toast.makeText(TransferActivity.this,result,Toast.LENGTH_LONG).show();
                                                                }
                                                            });
                                                        }
                                                        else{
                                                            runOnUiThread(new Runnable() {
                                                                @Override
                                                                public void run() {
                                                                    Toast.makeText(TransferActivity.this,"请求失败",Toast.LENGTH_LONG).show();
                                                                }
                                                            });
                                                        }
                                                    };

                                                }.start();
                                                Jishi();
                                            }
                                        }
                                    }
                )
                .setCountDownMillis(60_000L)//倒计时60000ms
                .setCountDownColor(android.R.color.holo_blue_light,android.R.color.darker_gray)//不同状态字体颜色
                .start();
    }
    public void Transfer(View view) {
        final String phone=et_ptt.getText().toString().trim();
        final String code=tv_gyan.getText().toString().trim();
        String st="0";
        if(cb_qc.isChecked()){
            st="1";
        }
        else st="0";
        final String status=st;
        String a= String.valueOf(isMobileNO( phone));
        if (phone.length()==0){
            Toast.makeText(TransferActivity.this,"手机号码不能为空！",Toast.LENGTH_LONG).show();
        }
        else if(a.equals("false")||phone.length()<11){
            Toast.makeText(TransferActivity.this,"请输入正确的手机号码！",Toast.LENGTH_LONG).show();
        }
        else if(code.length()==0){
            Toast.makeText(TransferActivity.this,"验证码不能为空！",Toast.LENGTH_LONG).show();
        }
        else {
            new Thread(){
                public void run(){
                    final String result= TransferService.TransferByClientPost(phone,code,status,bid,token);
                    if (result!=null){
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                String errno=null;
                                Toast.makeText(TransferActivity.this,result,Toast.LENGTH_LONG).show();
                                try {
                                    JSONObject jsonObject = new JSONObject(result);
                                    //根据key 获取相应的value
                                    errno=jsonObject.getString("errno");
                                    if(errno.equals("0")){
                                        Toast.makeText(TransferActivity.this,"权限转让成功！",Toast.LENGTH_LONG).show();
                                    }
                                } catch (Exception e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }

                            }
                        });
                    }
                    else{
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(TransferActivity.this,"请求失败",Toast.LENGTH_LONG).show();
                            }
                        });
                    }
                };

            }.start();

        }
    }



    //使用正则表达式判断
    public static boolean isMobileNO(String mobiles) {
        //"[1]"代表第1位为数字1，"[358]"代表第二位可以为3、5、8中的一个，"\\d{9}"代表后面是可以是0～9的数字，有9位。
        String telRegex = "13\\d{9}|14[57]\\d{8}|15[012356789]\\d{8}|18[01256789]\\d{8}|17[0678]\\d{8}"; ;
        if (TextUtils.isEmpty(mobiles)) return false ;
        else return mobiles.matches( telRegex ) ;
    }
    //获取验证码信息,进行计时操作
    private void startCountBack() {
        ((Activity) mContext).runOnUiThread(new Runnable() {
            @Override
            public void run() {
                tv_gyan.setText(countSeconds + "");
                mCountHandler.sendEmptyMessage(0);
            }
        });
    }
    public void Caidan1(View view){
        Intent intent=new Intent(this,MainActivity.class);
        intent.putExtra("flag",1);
        startActivity(intent);
    }
    public void Caidan2(View view){
        Intent intent=new Intent(this,MainActivity.class);
        intent.putExtra("flag",2);
        startActivity(intent);
    }
    public void Caidan3(View view){
        Intent intent=new Intent(this,MainActivity.class);
        intent.putExtra("flag",3);
        startActivity(intent);
    }
    public void Caidan5(View view){
        Intent intent=new Intent(this,MainActivity.class);
        intent.putExtra("flag",4);
        startActivity(intent);
    }
    public void Finish(View view){
        finish();
    }
    @Override
    public void onClick(View view) {

    }

}
